﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Repository.EntityFramework;

namespace Communergy.Repository
{
    public class EntityHistoricRepository : IHistoricRepository
    {
        communergyEntities db;

        public EntityHistoricRepository(IUnitOfWork unitOfWork)
        {
            //will return null if not communergyEntities
            db = unitOfWork as communergyEntities;
            if (db == null)
                throw new ArgumentOutOfRangeException("unitOfWork");

        }



        #region IAutomaticRepository Members

        public void Insert_ElectricityHourly(string meterId, List<Communergy.Common.HistoricalEnergyReading> hourlyEnergyReadings)
        {
            var meterIdGuid = new Guid(meterId);
            //using (var db = new communergyEntities(connString))
            //{
            var thisMeter = db.ElectricityMeter.Include("ElectricityHourly_Auto").Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
            if (thisMeter != null)
            {
                //only proceed if exists
                foreach (var energyReading in hourlyEnergyReadings)
                {
                    //using (var dbSave = new communergyEntities(connString))
                    //{
                    thisMeter.ElectricityHourly_Auto.Add(new ElectricityHourly_Auto
                    {
                        ah_hour = energyReading.ReadingDateTime,
                        ah_kWh = energyReading.KWh
                    });

                }
                // }
            }
        }

        public void Insert_ElectricityDaily(string meterId, List<HistoricalEnergyReading> dailyEnergyReadings)
        {
            var meterIdGuid = new Guid(meterId);
            //using (var db = new communergyEntities(connString))
            {
                var thisMeter = db.ElectricityMeter.Include("ElectricityDaily_Auto").Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
                if (thisMeter != null)
                {
                    //only proceed if exists
                    foreach (var energyReading in dailyEnergyReadings)
                    {
                        //using (var dbSave = new communergyEntities(connString))
                        {
                            thisMeter.ElectricityDaily_Auto.Add(new ElectricityDaily_Auto
                            {
                                ad_day = energyReading.ReadingDateTime.Date,
                                ad_kWh = energyReading.KWh,
                            });
                        }
                    }
                }
            }
        }

        public void Insert_ElectricityMonthly(string meterId, List<HistoricalEnergyReading> monthlyEnergyReadings)
        {
            var meterIdGuid = new Guid(meterId);
            //using (var db = new communergyEntities(connString))
            {
                var thisMeter = db.ElectricityMeter.Include("ElectricityMonthly_Auto").Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
                if (thisMeter != null)
                {
                    //only proceed if exists
                    foreach (var energyReading in monthlyEnergyReadings)
                    {
                        //TODO - fix for EF - use stored proc
                        //using (var dbSave = new communergyEntities(connString))
                        {
                            //use add
                            thisMeter.ElectricityMonthly_Auto.Add(new ElectricityMonthly_Auto
                            {
                                am_month = energyReading.ReadingDateTime.Date,
                                am_kWh = energyReading.KWh
                            });
                        }
                    }
                }
            }
        }

        #endregion

        public List<HistoricalEnergyReading> GetHourly(string meterId, DateTime dateFrom)
        {
            throw new NotImplementedException();
        }
    }
}
